<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="轮播图名称" prop="rotationName">
      <el-input v-model="dataForm.rotationName" placeholder="轮播图名称"></el-input>
    </el-form-item>
    <el-form-item label="轮播图地址" prop="rotationUrl">
<!--      <el-input v-model="dataForm.rotationUrl" placeholder="轮播图地址"></el-input>-->
      <el-upload
        :action="uploadImgUrl"
        list-type="picture-card"
        :on-success="handleUploadSuccess"
        :before-upload="handleBeforeUpload"
        :on-error="handleUploadError"
        name="file"
        :show-file-list="false"
        style="display: inline-block; vertical-align: top"
      >
        <img v-if="dataForm.rotationUrl" :src="dataForm.rotationUrl" class="avatar" />
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>
    </el-form-item>
    <el-form-item label="排序码" prop="rotationSort">
      <el-input v-model="dataForm.rotationSort" readonly="true" placeholder="排序码"></el-input>
    </el-form-item>
    <!-- <el-form-item label="创建时间" prop="createTime">
    <el-date-picker
            v-model="dataForm.createTime"
            type="datetime"
            format="yyyy-MM-dd HH:mm:ss"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择日期时间">
    </el-date-picker> -->
      <!-- <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input> -->
    <!-- </el-form-item>
    <el-form-item label="更新时间" prop="updateTime">
          <el-date-picker
            v-model="dataForm.updateTime"
            type="datetime"
            format="yyyy-MM-dd HH:mm:ss"
            value-format="yyyy-MM-dd HH:mm:ss1"
            placeholder="选择日期时间">
    </el-date-picker> -->
      <!-- <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input> -->
    <!-- </el-form-item> -->
    <el-form-item label="是否删除" prop="delFlag">
      <!-- <el-input v-model="dataForm.delFlag" placeholder="是否删除 0：存在 1：已删除"></el-input> -->
      	<el-select v-model="dataForm.delFlag" placeholder="是否删除 0：存在 1：已删除">
					<el-option label="存在" value="0"></el-option>
					<el-option label="删除" value="1"></el-option>
				</el-select>
    </el-form-item>
    <el-form-item label="状态" prop="status">
      <!-- <el-input v-model="dataForm.status" placeholder="状态 0：可用 1：不可用"></el-input> -->
            	<el-select v-model="dataForm.status" placeholder="状态 0：可用 1：不可用">
					<el-option label="可用" value="0"></el-option>
					<el-option label="不可用" value="1"></el-option>
				</el-select>
    </el-form-item>
    <el-form-item label="创建者" prop="createUser">
      <el-input v-model="dataForm.createUser" readonly="readonly"  placeholder="默认为操作者，可以不用输入"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import http from '../../../utils/httpRequest'
  import Vue from 'vue'

  export default {
    data () {
      return {
        // http.interceptors.request.use
        headers: {
          Authorization: Vue.cookie.get('token')
        },
        uploadImgUrl:  this.$http.adornUrl(`/sys/oss/upload?token=${this.$cookie.get('token')}`),
        visible: false,
        dataForm: {
          rotationId: 0,
          rotationName: '',
          rotationUrl: '',
          rotationSort: '',
          createTime: '',
          updateTime: '',
          delFlag: '',
          status: '',
          createUser: ''
        },
        dataRule: {
          rotationName: [
            { required: true, message: '轮播图名称不能为空', trigger: 'blur' }
          ],
          rotationUrl: [
            { required: true, message: '轮播图地址不能为空', trigger: 'blur' }
          ],
          rotationSort: [
            { required: true, message: '排序码不能为空', trigger: 'blur' }
          ],
          // createTime: [
          //   { required: true, message: '创建时间不能为空', trigger: 'blur' }
          // ],
          // updateTime: [
          //   { required: true, message: '更新时间不能为空', trigger: 'blur' }
          // ],
          delFlag: [
            { required: true, message: '是否删除 0：存在 1：已删除不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态 0：不可用 1：可用不能为空', trigger: 'blur' }
          ]
          // createUser: [
          //   { required: true, message: '创建者不能为空', trigger: 'blur' }
          // ]
        }
      }
    },
    // props: {
    //   value: {
    //     type: String,
    //     default: "",
    //   },
    // },
    methods: {
      handleUploadSuccess(res) {
        this.$emit("input", res.url);
        this.dataForm.rotationUrl=res.url;
        this.loading.close();
      },
      handleBeforeUpload() {
        this.loading = this.$loading({
          lock: true,
          text: "上传中",
          background: "rgba(0, 0, 0, 0.7)",
        });
      },
      handleUploadError() {
        this.$message({
          type: "error",
          message: "上传失败",
        });
        this.loading.close();
      },
      init (id) {
        this.dataForm.rotationId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.rotationId) {
            this.$http({
              url: this.$http.adornUrl(`/generator/webrotation/info/${this.dataForm.rotationId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.rotationName = data.webRotation.rotationName
                this.dataForm.rotationUrl = data.webRotation.rotationUrl
                this.dataForm.rotationSort = data.webRotation.rotationSort
                this.dataForm.createTime = data.webRotation.createTime
                this.dataForm.updateTime = data.webRotation.updateTime
                this.dataForm.delFlag = data.webRotation.delFlag
                this.dataForm.status = data.webRotation.status
                this.dataForm.createUser = data.webRotation.createUser
              }
            })
          }
          else{
            console.log('新增轮播图');
             this.$http({
              url: this.$http.adornUrl(`/generator/webrotation/getMaxSort`),
              method: 'get'
            }).then(({data}) => {
              if (data!=null) {
                 console.log('最大排序码是：'+data);
                 this.dataForm.rotationSort=data+1;

              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/generator/webrotation/${!this.dataForm.rotationId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'rotationId': this.dataForm.rotationId || undefined,
                'rotationName': this.dataForm.rotationName,
                'rotationUrl': this.dataForm.rotationUrl,
                'rotationSort': this.dataForm.rotationSort,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime,
                'delFlag': this.dataForm.delFlag,
                'status': this.dataForm.status,
                'createUser': this.dataForm.createUser
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>

<style scoped lang="scss">
.avatar {
  width: 100%;
  height: 100%;
}
</style>
